<?php
/*
 * 生成地区编码
 * @package    system
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: c1bee2577c0ed45b273f86fa9007e5e5a3820fe5 $
 */
error_reporting(E_ALL);
$startTime = microtime(true);
require_once '../../common.inc.php';
$db = new PDO('mysql:host=192.168.1.19;dbname=caiji', 'root', 'xddlxdcw');
$db->query('set names utf8');
//$areaCode = ToolsCard::getAreaCode();
//$totalNum = ToolsCard::provNum();
/*
foreach ($areaCode as $areaid => $areaname) {
	$insert = ' insert into card (cardNum, cardPwd, areaid, areaname, cardPwdUpper) values';
	for($carnum = 1 ; $carnum <=$totalNum; $carnum++) {
		$pwd = ToolsCard::genPwd();
		$item = array(
			'cardNum' => ToolsCard::genCardNo($areaid, $carnum),
			'cardPwd' => $pwd,
			'cardPwdUpper' => strtoupper($pwd),
			'areaid'  => $areaid,
			'areaname'=> $areaname,
		);
		$insert .= "('{$item['cardNum']}','{$item['cardPwd']}','{$item['areaid']}','{$item['areaname']}', '{$item['cardPwdUpper']}'),";
	}
	$insert = rtrim($insert, ',');
	if (!$db->query($insert)) {
		exit($insert);
	}
}
*/

$insert = ' insert into owncard_card (cardNum, cardcode) values';
for($num = 1;$num <= 6000; $num++) {
	$pwd = ToolsCard::genPwd();
	$item = array(
		'cardNum' => ToolsCard::genCardNo($num),
		'cardcode' => $pwd,
	);
	$insert .= "('{$item['cardNum']}','{$item['cardcode']}'),";
}
$insert = rtrim($insert, ',');
if (!$db->query($insert)) {
	exit($insert);
}
var_dump(microtime(true)-$startTime);
















class ToolsCard {

	public static function genPwd() {
		return strtolower(Str::random(6));
	}

	public static function provNum() {
		return 5000;
	}

	public static function getAreaCode() {
		return array(
			'11' => '北京市',
			'12' => '天津市',
			'13' => '河北省',
			'14' => '山西省',
			'15' => '内蒙古自治区',
			'21' => '辽宁省',
			'22' => '吉林省',
			'23' => '黑龙江省',
			'31' => '上海市',
			'32' => '江苏省',
			'33' => '浙江省',
			'34' => '安徽省',
			'35' => '福建省',
			'36' => '江西省',
			'37' => '山东省',
			'41' => '河南省',
			'42' => '湖北省',
			'43' => '湖南省',
			'44' => '广东省',
			'45' => '广西壮族自治区',
			'46' => '海南省',
			'50' => '重庆市',
			'51' => '四川省',
			'52' => '贵州省',
			'53' => '云南省',
			'54' => '西藏自治区',
			'61' => '陕西省',
			'62' => '甘肃省',
			'63' => '青海省',
			'64' => '宁夏回族自治区',
			'65' => '新疆维吾尔自治区',
			'71' => '台湾省',
			'81' => '香港特别行政区',
			'82' => '澳门特别行政区'
		);
	}

	public static function genCode($areaCode, $carNum) {
		$cardNoA = '60'. $areaCode;
		switch($carNum){
			case 1 <= $carNum && $carNum <= 1000:
				$cardNoD = 555;
				break;
			case 1001 <= $carNum && $carNum <= 2000:
				$cardNoD = 666;
				break;
			case 2001 <= $carNum && $carNum <= 3000:
				$cardNoD = 777;
				break;
			case 3001 <= $carNum && $carNum <= 4000:
				$cardNoD = 888;
				break;
			case 4001 <= $carNum && $carNum <= 5000:
				$cardNoD = 999;
				break;
		}
		$carNum = str_pad($carNum, 4, 0, STR_PAD_LEFT);
		$cardNoC = substr($carNum, 0, 3);
		$cardNoD = substr($carNum, 3, 1). $cardNoD;
		$cardNo = sprintf("%s %s 0%s %s", $cardNoA, '0000', $cardNoC, $cardNoD);
		return $cardNo;
	}

	public static function genCardNo($num) {
		$prefix = '6010000';
		switch($num){
			case 1 <= $num && $num <= 1000:
				$cardNoD = 5;
				break;
			case 1001 <= $num && $num <= 2000:
				$cardNoD = 6;
				break;
			case 2001 <= $num && $num <= 3000:
				$cardNoD = 7;
				break;
			case 3001 <= $num && $num <= 4000:
				$cardNoD = 8;
				break;
			case 4001 <= $num && $num <= 6000:
				$cardNoD = 9;
				break;
		}
		$cardNum = str_pad($num.$cardNoD, 5, 0, STR_PAD_LEFT);
		$code = sprintf('%s%s',$prefix ,$cardNum);
		return $code;
	}
}